package main

import (
	"crypto/sha256"
	"crypto/sha512"
	"flag"
	"fmt"
	"strings"
)

func main() {

	flags := flag.String("flag", "sha256", "")
	str := flag.String("str", "x", "")
	flag.Parse()
	printSha(strings.ToUpper(*flags), *str)

	fmt.Printf("x 与 X 的Sha256不同的bit数量：%d\n", compareSha256("x", "X"))
}

//  compareSha256 比较不同bit的数目
func compareSha256(str1 string, str2 string) int {
	a := sha256.Sum256([]byte(str1))
	b := sha256.Sum256([]byte(str2))
	num := 0
	for i := 0; i < len(a); i++ {
		for m := 0; m < len(b); m++ {
			// 对比字节是否相同
			if (a[i] >> uint(m)) != (b[i] >> uint(m)) {
				num++
			}
		}
	}
	return num
}

func printSha(flag string, str string) {
	switch flag {
	case "SHA256":
		fmt.Printf("sha256: %s\n", sha256.Sum256([]byte(str)))
		break
	case "SHA384":
		fmt.Printf("sha384: %s\n", sha512.Sum384([]byte(str)))
		break
	case "SHA512":
		fmt.Printf("sha512: %s\n", sha512.Sum512([]byte(str)))
	default:
		fmt.Println("error")
	}
}
